home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / EMULATOR / GBDK_BIN / !GBDK / docs / GBDK < prev    next >
Text File  |  1997-02-18  |  20KB  |  719 lines

  1. +--------------------------------------------------------------------+
  2. | The following is an HTML file converted into text format. See      |
  3. | http://lsewww.epfl.ch/~felber/home.html for the original.          |
  4. +--------------------------------------------------------------------+
  5.  
  6. GameBoy Developer's Kit
  7.  
  8. Version 1.1
  9.  
  10. What is the GBDK?
  11.  
  12. The name may sound pretentious. But with the GBDK, you can develop
  13. your own programs for the GB system, either in C or in assembly. The
  14. GBDK includes a set of libraries for the most common requirements and
  15. generates image files for use with a real GB or with VGB.
  16.  
  17. Why making development tools for the GB?
  18.  
  19. Because the GB really is a nice system, with lots of good games. It is
  20. small, cheap, has a good autonomy and is quite powerful. It has even a
  21. serial port. Further more, it is not only aimed at games.
  22.  
  23. What is the GBDK composed of?
  24.  
  25. The GBDK is composed of:
  26.  
  27.       * A full featured C compiler (with the only limitation that a
  28.         floating point library has yet to be written)
  29.       * An assembler that generates relocatable code
  30.       * A linker that produces GB image files
  31.       * A set of basic libraries, with source code
  32.       * Some example programs in assembly and in C
  33.  
  34. Well, I have not written all these programs from scratch.
  35.  
  36. The compiler is based on lcc, a retargetable compiler for ANSI/ISO C.
  37. Their original version generates code for the SPARC, MIPS R3000, and
  38. Intel 386 and its successors. lcc is in production use at Princeton
  39. University and AT&T Bell Laboratories. The man page gives usage
  40. details.
  41.  
  42. The assembler and the linker are based on public domain programs
  43. developed by Alan R. Baldwin.
  44.  
  45. Which system does the GBDK run on?
  46.  
  47. The UNIX version of GBDK has been tested on Sun Solaris 2.4 and Linux.
  48. The DOS version requires a 386 processor (or higher).
  49.  
  50. Limitations
  51.  
  52.       * The C compiler is missing support for floats and doubles (the
  53.         compiler supports them, but libraries are missing. If someone
  54.         is interested in writing them...)
  55.       * The linker generates only 32kb images for the time. Generating
  56.         64kb images is not a problem, but bigger images require bank
  57.         switching.
  58.       * Do not use -0x8000 (minimum 16-bit signed integer) in
  59.         divisions. -0x7FFF is the limit.
  60.  
  61. Other sites with info on the GB
  62.  
  63.       * Nintendo GameBoy Homepage
  64.         http://www.freeflight.com/fms/GameBoy/
  65.       * Jeff Frohwein's Technical Information Page
  66.         http://fly.hiwaay.net/~jfrohwei/gameboy/home.html
  67.  
  68. ----------------------------------------------------------------------
  69.  
  70. Changes
  71.  
  72. Changes in version 1.1
  73.  
  74.       * Removed Xloadimage from the GBDK distribution. It is now
  75.         available as a separate archive.
  76.       * A compiled DOS version is now available (cross-compiled on my
  77.         Sun Workstation!).
  78.       * The libraries and the example programs have been improved.
  79.       * The make script has been improved. Compiling on UNIX should be
  80.         easier.
  81.       * Many bugfixes.
  82.  
  83. ----------------------------------------------------------------------
  84.  
  85. The Compiler
  86.  
  87. I've written a code generator for lcc that generates code for the Z80.
  88. It does not produce optimal code, but it is usable. It took a long
  89. time to debug, but I think it is now quite stable. Note than due to
  90. the limitations of the Z80, sizeof(int) = sizeof(long) = 2.
  91.  
  92. For more information, read the docs included with the lcc
  93. distribution.
  94.  
  95. The following flags allow to pass options to the assembler and to the
  96. linker:
  97.  
  98.     -Wa 
  99.     -Wl 
  100.  
  101. If the assembler generates an error message, you can produce an
  102. assembly listing .lst to see where the error occurs using the flag:
  103.  
  104.     -Wa-l 
  105.  
  106. If you want to see the memory mop of the image file (where the
  107. functions are located in ROM), you can produce a .map file using:
  108.  
  109.     -Wl-m 
  110.  
  111. ----------------------------------------------------------------------
  112.  
  113. The Assembler
  114.  
  115. The GB processor is very similar to the Z80, although some of the
  116. instructions are missing and some ther have been added. Also, she
  117. second set of registers (BC', DE', HL', AF') and the index registers
  118. (IX, IY) are missing and, consequently, there are no DD and FD opcode
  119. tables. Finally, I/O ports are gone and so are all IN/OUT opcodes.
  120.  
  121. Changed instructions (from the GameBoy FAQ, by Marat Fayzullin):
  122.  
  123.     08 xx xx   EX AF,AF'      LD (word),SP     Save SP at given address
  124.     10 xx      DJNZ offset    STOP             Meaning unknown
  125.     22         LD (word),HL   LD (HLI),A       Save A at (HL) and increment HL
  126.     2A         LD HL,(word)   LD A,(HLI)       Load A from (HL) and increment HL
  127.     32         LD (word),A    LD (HLD),A       Save A at (HL) and decrement HL
  128.     3A         LD A,(word)    LD A,(HLD)       Load A from (HL) and decrement HL
  129.     D3         OUTA (byte)    No operation
  130.     D9         EXX            RETI             Enable interrupts and return
  131.     DB         INA (byte)     No operation
  132.     DD         Prefix DD      No operation
  133.     E0 xx      RET PO         LD (byte),A      Save A at (FF00+byte)
  134.     E2         JP PO,word     LD (C),A         Save A at (FF00+C)
  135.     E3         EX HL,(SP)     No operation
  136.     E4         CALL PO,word   No operation
  137.     E8 xx      RET PE         ADD SP,offset    Add signed offset to SP
  138.     EA xx xx   JP PE,word     LD (word),A      Save A at given address
  139.     EB         EX DE,HL       No operation
  140.     EC         CALL PE,word   No operation
  141.     F0 xx      RET P          LD A,(byte)      Load A from (FF00+byte)
  142.     F2         JP P,word      No operation
  143.     F4         CALL P,word    No operation
  144.     F8 xx      RET M          LDHL SP,offset   Load HL with SP + signed offset
  145.     FA xx xx   JP M,word      LD A,(word)      Load A from given address
  146.     FC         CALL M,word    No operation
  147.     FD         Prefix FD      No operation
  148.  
  149. I have also modified the name of some of the GB-specific opcodes:
  150.  
  151.     LD (HLI),A      -> LD (HL+),A
  152.     LD (HLD),A      -> LD A,(HL-)
  153.     LD A,(HLI)      -> LD (HL+),A
  154.     LD A,(HLD)      -> LD A,(HL-)
  155.     ADD SP,offset   -> LDA SP,offset(SP)
  156.     LDHL SP,offset  -> LDA HL,offset(SP)
  157.  
  158. The LDA opcode means "load address", like in 68x00 assembly. I've
  159. called these instructions like this because both are orthogonal (they
  160. do the same thing on two different registers), and anyway I hate the
  161. LDHL (just because the destination register is specified in the
  162. opcode).
  163.  
  164. The assembler accepts the following flags:
  165.  
  166.     Usage: [-vdqxcgalosf] [-n filename] file1 [file2 file3 ...]
  167.       v    verbose
  168.       d    decimal  listing
  169.       q    octal    listing
  170.       x    hex      listing (default)
  171.       k    case sensitive
  172.       g    undefined symbols made global
  173.       a    all user symbols made global
  174.       l    create list   output file[LST]
  175.       o    create object output file[O]
  176.       s    create symbol output file[SYM]
  177.       f    flag relocatable references by  `   in listing file
  178.      ff    flag relocatable references by mode in listing file
  179.       n    name of output files (for following input file)
  180.  
  181. For more information, read the asmlnk.doc file in the doc directory.
  182.  
  183. ----------------------------------------------------------------------
  184.  
  185. The Linker
  186.  
  187. The linker accepts the following flags:
  188.  
  189.     Usage: [-options] -o outfile [file.o ... | @file.lst]
  190.       @file.lst      file with list of files to link, separated by newlines
  191.       -c             case sensitive
  192.       -v             verbose
  193.     Relocation:
  194.       -b             area base address = expression
  195.       -g             global symbol = expression
  196.     Map format:
  197.       -m             map output generated as file[MAP]
  198.       -x             hexidecimal (default)
  199.       -d             decimal
  200.       -q             octal
  201.     Output:
  202.       -i             Intel hex as file[IHX]
  203.       -s             Motorola s19 as file[S19]
  204.       -z             Gameboy image as file[GB]
  205.  
  206. For more information, read the asmlnk.doc file in the doc directory.
  207.  
  208. ----------------------------------------------------------------------
  209.  
  210. The Libraries
  211.  
  212. Three libraries are included in the GBDK. Their functions are
  213. described in details below.
  214.  
  215. crt0.o
  216.  
  217. Basic C runtime, with GB initialization routines, C support (mul, div,
  218. mod) and other essential things. This library is automatically linked
  219. with every program.
  220.  
  221. terminal.o - stdio.o
  222.  
  223. Libraries for basic text input/output. Implements standard functions
  224. from stdio, ctype and string.
  225.  
  226. drawing.o
  227.  
  228. Very primitive graphic library that allows to draw points to the
  229. screen, and to display images. The drawing area is limited because of
  230. the way the GB handles display.
  231.  
  232.  
  233. ----------------------------------------------------------------------
  234.  
  235. stdlib.o
  236.  
  237. Include files
  238.  
  239.       * stdlib.h
  240.  
  241. Source files
  242.  
  243.       * crt0.s
  244.  
  245. Functions
  246.  
  247. void mode(int m);
  248.  
  249.             Change current working mode (M_DRAWING or M_TEXT). This is
  250.             normally implicitly done when using library functions.
  251.  
  252. void delay(int d);
  253.  
  254.             Small pause.
  255.  
  256. void pause(int p);
  257.  
  258.             Longer pause.
  259.  
  260. int joypad();
  261.  
  262.             Read the joypad status. Joypad keys are J_START, J_SELECT,
  263.             J_B, J_A, J_DOWN, J_UP, J_LEFT and J_RIGHT.
  264.  
  265. int waitpad(int mask);
  266.  
  267.             Wait for one of the specified joypad keys to be pressed.
  268.  
  269. void waitpadup();
  270.  
  271.             Wait for the joypad to be released.
  272.  
  273. void enable_interrupts();
  274. void disable_interrupts();
  275.  
  276.             Enable or Disable interrupts (must be enabled for
  277.             displaying sprites).
  278.  
  279. void display_on();
  280. void display_off();
  281.  
  282.             Switch screen on or off.
  283.  
  284. void show_bkg();
  285. void hide_bkg();
  286.  
  287.             Show or hide the background display.
  288.  
  289. void set_bkg_data(int first_tile, int nb_tiles, unsigned char *data);
  290.  
  291.             Set the data of part of the background tiles.
  292.  
  293.       -128 <= first_tile <= 127
  294.       -128 <= first_tile+nb_tiles <= 127
  295.       
  296.       nb_tiles >= 1
  297.  
  298. void set_bkg_tiles(int x, int y, int w, int h, unsigned char *tilelist);
  299.  
  300.             Set the tile number of part of the background.
  301.  
  302.       0 <= x <= 31
  303.       0 <= y <= 31
  304.       1 <= w <= 32-x
  305.       
  306.       1 <= h <= 32-y
  307.  
  308. void scroll_bkg(int x, int y);
  309.  
  310.             Scroll the background (relative to current position).
  311.  
  312. void show_window();
  313. void hide_window();
  314.  
  315.             Show or hide the window display.
  316.  
  317. void show_sprites();
  318. void hide_sprites();
  319.  
  320.             Show or hide the sprites display.
  321.  
  322. void sprites8x8();
  323. void sprites8x16();
  324.  
  325.             Set the size of all sprites.
  326.  
  327. void set_sprite_data(int first_tile, int nb_tiles, unsigned char *data);
  328.  
  329.             Set the data of part of the sprite tiles.
  330.  
  331.       0 <= first_tile <= 255
  332.       0 <= first_tile+nb_tiles <= 255
  333.       
  334.       nb_tiles >= 1
  335.  
  336. void set_sprite_tile(int nb, int tile);
  337.  
  338.             Set the tile number of a sprite.
  339.  
  340.       0 <= nb <= 39
  341.       
  342.       0 <= tile <= 255
  343.  
  344. void set_sprite_prop(int nb, int prop);
  345.  
  346.             Set the properties of a sprite. Sprite properties bits are
  347.             S_PALETTE, S_FLIPX, S_FLIPY and S_PRIORITY.
  348.  
  349.       0 <= nb <= 39
  350.  
  351. void move_sprite(int nb, int x, int y);
  352.  
  353.             Change the position of a sprite.
  354.  
  355.       0 <= nb <= 39
  356.       0 <= x <= 255
  357.       
  358.       0 <= y <= 255
  359.  
  360. ----------------------------------------------------------------------
  361.  
  362. stdio.o - terminal.o
  363.  
  364. Include files
  365.  
  366.       * stdio.h
  367.  
  368. Source files
  369.  
  370.       * stdio.c
  371.       * terminal.s
  372.  
  373. Functions
  374.  
  375. int atoi(char *s);
  376.  
  377.             Return the integer value of a numeric string.
  378.  
  379. int abs(int num);
  380.  
  381.             Return the absolute value of an integer.
  382.  
  383. int isalpha(char c);
  384. int isupper(char c);
  385. int islower(char c);
  386. int isdigit(char c);
  387. int isspace(char c);
  388.  
  389.             Functions that classify character-coded integer values.
  390.  
  391. int toupper(char c);
  392. int tolower(char c);
  393.  
  394.             Change character case
  395.  
  396. int index(char *s, char *t);
  397.  
  398.             Find index of string t in s.
  399.  
  400. char *itoa(int n, char *s);
  401.  
  402.             Transform an integer in its ascii representation.
  403.  
  404. void printn(int number, int radix);
  405.  
  406.             Print a number in any radix.
  407.  
  408. char *reverse(char *s);
  409.  
  410.             Reverse a character string.
  411.  
  412. char *strcat(char *s1, char *s2);
  413.  
  414.             Concatenate s2 on the end of s1. s1 must be large enough.
  415.             Return s1.
  416.  
  417. int strcmp(char *s1, char *s2);
  418.  
  419.             Compare strings:
  420.  
  421.       s1>s2: >0
  422.       s1==s2: 0
  423.       
  424.       s1<s2: <0
  425.  
  426. char *strcpy(char *s1, char *s2);
  427.  
  428.             Copy string s2 to s1. s1 must be large enough. Return s1.
  429.  
  430. int strlen(char *s);
  431.  
  432.             Return length of string.
  433.  
  434. char *strncat(char *s1, char *s2, int n);
  435.  
  436.             Concatenate s2 on the end of s1. s1 must be large enough.
  437.             At most n characters are moved. Return s1.
  438.  
  439. int strncmp(char *s1, char *s2, int n);
  440.  
  441.             Compare strings (at most n bytes):
  442.  
  443.       s1>s2: >0
  444.       s1==s2: 0
  445.       
  446.       s1<s2: <0
  447.  
  448. char *strncpy(char *s1, char *s2, int n);
  449.  
  450.             Copy s2 to s1, truncating or null-padding to always copy n
  451.             bytes. Return s1.
  452.  
  453. void puts(char *str);
  454.  
  455.             Print a string with a carriage return.
  456.  
  457. void print(char *str);
  458.  
  459.             Print a string without carriage return.
  460.  
  461. void printf(char *fmt, ...);
  462. int scanf(char *fmt, ...);
  463.  
  464.             Print a fromatted string. printf and scanf support the
  465.             following types:
  466.  
  467.       %c char
  468.       %d decimal int
  469.       %o octal int
  470.       %p pointer
  471.       %s string
  472.       
  473.       %x hexadecimal int
  474.  
  475.       When waiting for a user input, a kind of keyboard appears at the
  476.       bottom of the screen, which allows to enter characters. The
  477.       following buttons are used:
  478.  
  479.       Arrow keys: Move the cursor
  480.       A: Enter a character
  481.       B: Delete a character
  482.       START: End of line (carriage return)
  483.       SELECT: Temporarily hide the keyboard
  484.  
  485. void putchar(char c);
  486.  
  487.             Print a character.
  488.  
  489. char getchar();
  490.  
  491.             Read a character.
  492.  
  493. char *gets(char *s);
  494.  
  495.             Read a string.
  496.  
  497. void gotoxy(int x, int y);
  498.  
  499.             Move the cursor to a specific position
  500.  
  501. int posx();
  502. int posy();
  503.  
  504.             Return the current cursor position
  505.  
  506. void setchar(char c);
  507.  
  508.             Set the character at cursor position, without character
  509.             interpretation ('\n' does not move to the next line) and
  510.             without moving the cursor.
  511.  
  512. ----------------------------------------------------------------------
  513.  
  514. drawing.o
  515.  
  516. Include files
  517.  
  518.       * graphics.h
  519.  
  520. Source files
  521.  
  522.       * drawing.s
  523.  
  524. Functions
  525.  
  526. void plot(int x, int y, int color, int mode);
  527.  
  528.             Draw a pixel on screen with specific color and mode.
  529.             Colors are WHITE, LTGREY, DKGREY and BLACK. Modes are AND,
  530.             OR, XOR and SOLID.
  531.  
  532. void draw_image(unsigned char *data);
  533.  
  534.             Draws a complete image to screen. Image size must be
  535.             0x80 * 0x78 pixels.
  536.  
  537. ----------------------------------------------------------------------
  538.  
  539. The Test and Example Programs
  540.  
  541. The test programs are located in the tst directory. Most of them are
  542. taken from the lcc distribution.  Expected output are located in the
  543. .out files.
  544.  
  545. 8q.c
  546.  
  547.             The classic 8 queens problem (place 8 queens on a
  548.             chessboard so that none of them threaten the others).
  549.  
  550. array.c
  551.  
  552.             Test program with arrays.
  553.  
  554. init.c
  555.  
  556.             Test program with variable initializations.
  557.  
  558. sort.c
  559.  
  560.             Sorting algorithm that uses arrays and pointers.
  561.  
  562. struct.c
  563.  
  564.             Test program with structures.
  565.  
  566. test.c
  567.  
  568.             Test program for terminal and drawing libraries.
  569.  
  570. There are also three interesting sample programs in the examples
  571. directory:
  572.  
  573. space.s
  574.  
  575.             Assembly program that demonstrates the use of sprites,
  576.             window, background, fixed-point values and more. The
  577.             following keys are used:
  578.  
  579.       Arrow keys: Change the speed (and direction) of the sprite
  580.       Arrow keys+A: Change the speed (and direction) of the window
  581.       Arrow keys+B: Change the speed (and direction) of the 
  582.       background
  583.       START: Open/close the door
  584.       SELECT: Basic fading effect
  585.  
  586. sound.c
  587.  
  588.             Program for experimenting with the soung generator of the
  589.             GB (to use on a real GB). The four different sound modes
  590.             of the GB are available. It also demonstrates the use of
  591.             bit fields in C (it's a quick hack, so don't expect too
  592.             much from the code). The following keys are used:
  593.  
  594.       UP/DOWN: Move the cursor
  595.       RIGHT/LEFT: Increment/decrement the value
  596.       RIGHT/LEFT+A: Increment/decrement the value by 10
  597.       RIGHT/LEFT+B: Set the value to maximum/minimum
  598.       START: Play the current mode's sound (or all modes if in 
  599.       control screen)
  600.       START+A: Play a little music with the current mode's sound
  601.       SELECT: Change the sound mode (1, 2, 3, 4 and control)
  602.       SELECT+A: Dump the sound registers to the screen
  603.  
  604. sprite.c
  605.  
  606.             Program that demonstrates the use of sprinte form C.
  607.  
  608. rpn.c
  609.  
  610.             Basic RPN calculator. Try entering expressions like 12
  611.             134* and then 1789+.
  612.  
  613. ----------------------------------------------------------------------
  614.  
  615. Mixing C and Assembly
  616.  
  617. For mixing C and assembly, you must use different files (you cannot
  618. embed C code with assembly) and link them together. Here are the
  619. things you must know:
  620.  
  621.       * A C identifier i will be called _i in assembly
  622.       * Results are always returned into the HL register
  623.       * Parameters are always passed on the stack (starting at SP+2
  624.         because the return address is also saved on the stack)
  625.       * Assembly identifier are exported using the .globl directive
  626.  
  627.       * You can access GameBoy hardware registers using _reg_XX where
  628.         XX is the register number (see sound.c for an real example)
  629.       * Registers must be preserved across function calls (you must
  630.         store them at function begin, and restore them at the end),
  631.         except HL.
  632.  
  633. Here is an example of how to mix assembly with C:
  634.  
  635. main.c
  636.  
  637.     main()
  638.     {
  639.       int i;
  640.       int add(int, int);
  641.  
  642.       i = add(1, 3);
  643.     }
  644.  
  645. add.s
  646.  
  647.     .globl _add
  648.     _add:           ; int add(int a, int b)
  649.       PUSH BC       ; Save used registers (except HL)
  650.       PUSH DE
  651.       LDA  HL,2(SP)
  652.       LD   C,(HL)   ; Get a
  653.       INC  HL
  654.       LD   B,(HL)
  655.       INC  HL
  656.       LD   E,(HL)   ; Get b
  657.       INC  HL
  658.       LD   D,(HL)
  659.       LD   H,D      ; Move DE into HL
  660.       LD   L,E
  661.       ADD  HL,BC    ; Add BC to HL
  662.       POP  DE       ; Restore registers
  663.       POP  BC
  664.       RET                  ; Return result into HL
  665.  
  666. ----------------------------------------------------------------------
  667.  
  668. Troubleshooting
  669.  
  670. Errors
  671.  
  672. Messages of the type:
  673.  
  674.     u 0226
  675.     a 0329
  676.     u 0333
  677.  
  678. are error messages from the assembler. To see where these errors
  679. occur, you should produce an assembly listing using the -Wa-l flag of
  680. lcc. An object file is generated, but must be corrupted. For more
  681. information on the different types of errors, read the asmlnk.doc file
  682. in the doc directory.
  683.  
  684. Messages of the type:
  685.  
  686.     ?ASlink-W-Undefined Global     .count referenced by module     Demo
  687.  
  688. are error messages from the linker. You probably forgot a library when
  689. linking, An image file is generated, but must be corrupted.
  690.  
  691. Warnings
  692.  
  693.       * Do not declare initialized variables at the file level, except
  694.         when they are read-only, because they will be located in ROM,
  695.         e.g.
  696.  
  697.     int i1;          /* OK    : will be located in RAM */ 
  698.     char *s1;        /* OK    : will be located in RAM */ 
  699.     int i2 = 0;      /* Error : will be located in ROM */ 
  700.     char *s2 = "Hi"; /* Error : will be located in ROM */ 
  701.  
  702.     void main() { ... } 
  703.  
  704.       * Both terminal.o and drawing.o libraries use a lot of tiles and
  705.         sprites from the GB. You should not use your own tiles or
  706.         sprites with these libraries.
  707.       * If you use both libraries in a same program, keep in mind that
  708.         there will be a "mode switch" when using a function from a
  709.         library after one of the other and all your work will be lost
  710.         (if in drawing mode you use a terminal function, your drawing
  711.         will be lost).
  712.  
  713. ----------------------------------------------------------------------
  714.  
  715. For suggestions, comments, bug reports... please mail to <felber@di.epfl.ch>.
  716.  
  717. ----------------------------------------------------------------------
  718. Last change: January 9, 1997 
  719.